﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListOfStudentsFile
{
    class Student
    {
        private string lastname;
        private string firstName;
        private string id;
        private string pathFile = @"C:\My files\ListOfStudents.txt";

        public Student()
        {

        }

        private Student(string id, string firstName, string lastname)
        {
            this.id = id;
            this.firstName = firstName;
            this.lastname = lastname;
        }

        public void FillValuesOfStudents()
        {
            Console.WriteLine("Введите фамилию студента: ");
            lastname = Console.ReadLine();
            Console.WriteLine("Введите имя студента: ");
            firstName = Console.ReadLine();
            Console.WriteLine("Введите номер студента: ");
            id = Console.ReadLine();
        }
        
        public void MakeListOfStudentsAndWriteItToFile()
        {
            string choice = "Y";
            List<Student> students = new List<Student>();

            do
            {
                Student student = new Student();
                student.FillValuesOfStudents();
                Console.WriteLine("Хотите добавить еще одного студента: ");
                choice = Console.ReadLine().ToUpper();
                students.Add(student);
            }
            while (choice == "Y");
            if (File.Exists(pathFile))
                File.Delete(pathFile);
            foreach (Student student in students)
            {
                File.AppendAllText(pathFile, student.id + "\t");
                File.AppendAllText(pathFile, student.firstName + "\t");
                File.AppendAllText(pathFile, student.lastname);
                File.AppendAllText(pathFile, "\n");
            }
        }

        public void ReadListOfStudentsFromFile()
        {
            List<Student> newStudentList = new List<Student>();
            string[] textFromFile = File.ReadAllLines(pathFile);
            for (int i = 0; i < textFromFile.Length; i++)
            {
                string[] studentInfo = textFromFile[i].Split('\t');
                newStudentList.Add(new Student(studentInfo[0], studentInfo[1], studentInfo[2]));
            }
            foreach (Student student in newStudentList)
            {
                Console.Write("Номер студента " + student.id + "\t");
                Console.Write("Имя студента " + student.firstName + "\t");
                Console.Write("Фамилия студента " + student.lastname);
                Console.WriteLine();
            }
        }
    }
}
